﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Data;

using System.Data.OracleClient;

public partial class mis_authorizor : System.Web.UI.Page
{
  MISEntities mis = new MISEntities();

  protected void Page_Load(object sender, EventArgs e)
  {
    if (!IsPostBack)
    {
      BuildTreeViewFunction();
      // 載入群組使用者清單
      //using (MISEntities dc = new MISEntities())
      //{
      //    var roles = dc.MIS_ROLE_AP.OrderBy(r => r.ROLE_NAME);
      //    TreeNode role_node;
      //    foreach (var role in roles)
      //    {
      //        role_node = new TreeNode();
      //        role_node.Value = role.ROLE_ID;
      //        role_node.Text = role.ROLE_NAME;
      //        TreeView_Role.Nodes.Add(role_node);

      //        var users = dc.MIS_USER_AP.Where(u=>u.ROLE_ID == role.ROLE_ID);
      //        TreeNode user_node;
      //        foreach (var user in users)
      //        {
      //            user_node = new TreeNode();
      //            user_node.Value = user.USER_ID;
      //            user_node.Text = user.USER_NAME;
      //            role_node.ChildNodes.Add(user_node);                                                
      //        }
      //    }
      //}
    }

  }

  protected void LoadUserTreeView()
  {
    TreeNode node = null;
    TreeNode dept2_node = null;
    TreeNode dept3_node = null;
    TreeNode dept4_node = null;
    TreeNode dept5_node = null;
    //載入使用者清單
    using (AS400Entities as400 = new AS400Entities())
    {
      //列出公司
      var depts = from b in as400.AS400_BIF41
                  join z in as400.AS400_ZDF306 on b.BI4101 equals z.F30600
                  where b.BI4101 == b.BI4119
                  select new { b.BI4101, z.F30603 };
      var depts2 = from b in as400.AS400_BIF41
                   join z in as400.AS400_ZDF306 on b.BI4101 equals z.F30600
                   where b.BI4101 == b.BI4115
                   select new { b.BI4101, z.F30603, b.BI4119 };
      var depts3 = from b in as400.AS400_BIF41
                   join z in as400.AS400_ZDF306 on b.BI4101 equals z.F30600
                   where b.BI4101 == b.BI4114
                   select new { b.BI4101, z.F30603, b.BI4115 };
      var depts4 = from b in as400.AS400_BIF41
                   join z in as400.AS400_ZDF306 on b.BI4101 equals z.F30600
                   where b.BI4101 == b.BI4113
                   select new { b.BI4101, z.F30603, b.BI4114 };
      var depts5 = from b in as400.AS400_BIF41
                   join z in as400.AS400_ZDF306 on b.BI4101 equals z.F30600
                   where b.BI4101 == b.BI4112
                   select new { b.BI4101, z.F30603, b.BI4113 };

      foreach (var item in depts)
      {
        //新增USER                    
        node = BuildDeptTreeView(TreeView_Users, item.F30603, item.BI4101);
        AddUserToTreeNodeByDept(node, item.BI4101);
        //列出營級
        var dept2s = from b in depts2 where b.BI4119 == item.BI4101 select b;
        foreach (var dept2 in dept2s)
        {
          dept2_node = BuildDeptTreeNode(node, dept2.F30603, dept2.BI4101);
          AddUserToTreeNodeByDept(dept2_node, dept2.BI4101);
          //列出處級
          var dept3s = from b in depts3 where b.BI4115 == dept2.BI4101 select b;
          foreach (var dept3 in dept3s)
          {
            dept3_node = BuildDeptTreeNode(dept2_node, dept3.F30603, dept3.BI4101);
            AddUserToTreeNodeByDept(dept3_node, dept3.BI4101);
            //列出站級
            var dept4s = from b in depts4 where b.BI4114 == dept3.BI4101 select b;
            foreach (var dept4 in dept4s)
            {
              dept4_node = BuildDeptTreeNode(dept3_node, dept4.F30603, dept4.BI4101);
              AddUserToTreeNodeByDept(dept4_node, dept4.BI4101);
              //列出所級
              var dept5s = from b in depts5 where b.BI4113 == dept4.BI4101 select b;
              foreach (var dept5 in dept5s)
              {
                dept5_node = BuildDeptTreeNode(dept4_node, dept5.F30603, dept5.BI4101);
                AddUserToTreeNodeByDept(dept5_node, dept5.BI4101);
              }
            }
          }
        }
      }
    }
  }

  protected void BuildTreeViewFunction()
  {
    TreeViewUtil tvu = new TreeViewUtil(Treeview_Function);
    //使用RoleID來識別MIS部門或是授權代理人
    var user = mis.MIS_USER_AP.Where(u => u.USER_ID == User.Identity.Name).SingleOrDefault();
    if (user.ROLE_ID == "Administrators") //IT部門
    {
      tvu.BuildTreeView(null);
    }
    else if (user.ROLE_ID == "Users") //IT授權給代理人設定權限
    {
      tvu.BuildTreeViewByUser(null);
    }
    Treeview_Function.ExpandAll();
    Treeview_Function.Attributes.Add("onclick", "OnTreeClick(event)");

    foreach (TreeNode node in Treeview_Function.Nodes)
    {
      node.NavigateUrl = "javascript:void(0);";
      foreach (TreeNode cNode in node.ChildNodes)
      {
        cNode.NavigateUrl = "javascript:void(0);";
      }
    }
  }

  protected void AddUserToTreeNodeByDept(TreeNode parent_node, string dept_id)
  {
    var users = mis.MIS_USER_AP.Where(u => u.DEPT_ID == dept_id);
    foreach (var user in users)
    {
      TreeNode user_node = new TreeNode();
      user_node.Value = user.USER_ID;
      user_node.Text = user.USER_NAME.Trim() + "[" + user.USER_ID + "]";
      user_node.ShowCheckBox = true;
      user_node.ImageUrl = "~/Images/user.png";
      parent_node.ChildNodes.Add(user_node);
    }
  }

  protected void AddUserToTreeNodeByUserID(TreeNode parent_node, string user_id)
  {
    var user = mis.MIS_USER_AP.Where(u => u.USER_ID == user_id).SingleOrDefault();
    if (user != null)
    {
      TreeNode user_node = new TreeNode();
      user_node.Value = user.USER_ID;
      user_node.Text = user.USER_NAME.Trim() + "[" + user.USER_ID + "]";
      user_node.ShowCheckBox = true;
      user_node.ImageUrl = "~/Images/user.png";
      parent_node.ChildNodes.Add(user_node);
    }
  }

  protected TreeNode BuildDeptTreeView(TreeView tv, string key, string value)
  {
    TreeNode dept_node = new TreeNode();
    dept_node.Value = value;
    dept_node.Text = key;
    dept_node.ShowCheckBox = false;
    dept_node.NavigateUrl = "javascript:void(0);";
    dept_node.ImageUrl = "~/Images/roles.png";
    tv.Nodes.Add(dept_node);
    return dept_node;
  }

  protected TreeNode BuildDeptTreeNode(TreeNode parent_node, string key, string value)
  {
    TreeNode dept_node = new TreeNode();
    dept_node.Value = value;
    dept_node.Text = key;
    dept_node.ShowCheckBox = false;
    dept_node.NavigateUrl = "javascript:void(0);";
    dept_node.ImageUrl = "~/Images/roles.png";
    parent_node.ChildNodes.Add(dept_node);
    return dept_node;
  }



  protected void Treeview_Function_TreeNodeCheckChanged(object sender, TreeNodeEventArgs e)
  {
    foreach (GridViewRow row in gv_function_control.Rows)
    {
      Label lbl_function_id = row.FindControl("lbl_function_id") as Label;
      if (e.Node.Value == lbl_function_id.Text)
      {
        row.Visible = e.Node.Checked;
      }
    }
  }
  protected void btn_save_Click(object sender, EventArgs e)
  {
    string userID = string.Empty;
    string functionID = string.Empty;
    Label lbl_function_id = null;
    CheckBoxList cbList = null;
    MIS_AUTHORIZOR_AP authorizor = null;
    IQueryable<MIS_AUTHORIZOR_AP> del_auths = null;

    if (TreeView_Users.CheckedNodes.Count == 0)
    {
      CommonUtil.DisplayErrorMessage("請至少挑選一個使用者設定!", lbl_message);
      return;
    }

    try
    {
      //先找到勾選的USER
      foreach (TreeNode usernode in TreeView_Users.CheckedNodes)
      {
        userID = usernode.Value;
        //先刪除User針對同一Function的控制項權限
        del_auths = mis.MIS_AUTHORIZOR_AP.Where(a => a.USER_ID == userID);
        if (del_auths.Count() > 0)
        {
          foreach (var auths in del_auths)
          {
            mis.MIS_AUTHORIZOR_AP.Remove(auths);
          }
        }
        //再找勾選的選單Function
        foreach (TreeNode functionNode in Treeview_Function.CheckedNodes)
        {
          functionID = functionNode.Value;
          string funType = mis.MIS_FUNCTION_AP
              .Where(f => f.FUNCTION_ID == functionID)
              .SingleOrDefault().FUNCTION_TYPE;
          if (funType.ToUpper() == "FUNCTION")
          {
            //比對選單對應控制項的GridView，找出符合項目
            foreach (GridViewRow row in gv_function_control.Rows)
            {
              lbl_function_id = row.FindControl("lbl_function_id") as Label;
              if (lbl_function_id.Text == functionID)
              {
                //取出有勾選的功能控制項
                cbList = row.FindControl("cb_control") as CheckBoxList;

                var controlIDs = cbList.Items.Cast<ListItem>()
                    .Where(c => c.Selected).Select(c => c.Value).ToArray<string>();
                if (controlIDs.Count() > 0)
                {
                  foreach (var item in controlIDs)
                  {
                    //新增User針對Function的控制項權限                                    
                    authorizor = new MIS_AUTHORIZOR_AP();
                    authorizor.AUTH_ID = DateTime.Now.ToString("yyyyMMdd-hhmmss-ff");
                    authorizor.USER_ID = userID;
                    authorizor.FUNCTION_ID = functionID;
                    authorizor.CONTROL_ID = item;
                    authorizor.REMARK = string.Empty;
                    authorizor.CRT_ID = User.Identity.Name;
                    authorizor.CRT_DATE = DateTime.Now.ToString("yyyyMMdd");
                    authorizor.CRT_TIME = DateTime.Now.ToString("hhmmss");
                    authorizor.UPD_ID = User.Identity.Name;
                    authorizor.UPD_DATE = DateTime.Now.ToString("yyyyMMdd");
                    authorizor.UPD_TIME = DateTime.Now.ToString("hhmmss");
                    mis.MIS_AUTHORIZOR_AP.Add(authorizor);
                  }
                }
                else
                {
                  authorizor = new MIS_AUTHORIZOR_AP();
                  authorizor.AUTH_ID = DateTime.Now.ToString("yyyyMMdd-hhmmss-ff");
                  authorizor.USER_ID = userID;
                  authorizor.FUNCTION_ID = functionID;
                  authorizor.CONTROL_ID = " ";
                  authorizor.REMARK = string.Empty;
                  authorizor.CRT_ID = User.Identity.Name;
                  authorizor.CRT_DATE = DateTime.Now.ToString("yyyyMMdd");
                  authorizor.CRT_TIME = DateTime.Now.ToString("hhmmss");
                  authorizor.UPD_ID = User.Identity.Name;
                  authorizor.UPD_DATE = DateTime.Now.ToString("yyyyMMdd");
                  authorizor.UPD_TIME = DateTime.Now.ToString("hhmmss");
                  mis.MIS_AUTHORIZOR_AP.Add(authorizor);
                }
              }
            }
          }
          else
          {
            //若選單為資料夾類型，則控制項不新增，給預設值：999                         
            authorizor = new MIS_AUTHORIZOR_AP();
            authorizor.AUTH_ID = DateTime.Now.ToString("yyyyMMdd-hhmmss-ff");
            authorizor.USER_ID = userID;
            authorizor.FUNCTION_ID = functionID;
            authorizor.CONTROL_ID = "999";
            authorizor.REMARK = string.Empty;
            authorizor.CRT_ID = User.Identity.Name;
            authorizor.CRT_DATE = DateTime.Now.ToString("yyyyMMdd");
            authorizor.CRT_TIME = DateTime.Now.ToString("hhmmss");
            authorizor.UPD_ID = User.Identity.Name;
            authorizor.UPD_DATE = DateTime.Now.ToString("yyyyMMdd");
            authorizor.UPD_TIME = DateTime.Now.ToString("hhmmss");
            mis.MIS_AUTHORIZOR_AP.Add(authorizor);
          }
        }
      }
      //同步更新資料庫
      mis.SaveChanges();
      CommonUtil.DisplayInfoMessage("使用者選單權限暨控制項功能設定成功!", lbl_message);
    }
    catch (Exception ex)
    {
      CommonUtil.DisplayErrorMessage(ex.Message, lbl_message);
    }
  }
  protected void TreeView_Users_SelectedNodeChanged(object sender, EventArgs e)
  {
    try
    {
      //重新整理TreeView&GridView (為了控管畫面呈現連動)
      BuildTreeViewFunction();

      string userid = TreeView_Users.SelectedNode.Value;
      string username = mis.MIS_USER_AP.Where(u => u.USER_ID == userid).Single().USER_NAME.Trim();
      var auths = from a in mis.MIS_AUTHORIZOR_AP
                  where a.USER_ID == userid
                  select new { a.FUNCTION_ID };
      //找尋選單
      foreach (var fun in auths)
      {
        TreeNode query = (from node in Treeview_Function.Nodes.OfType<TreeNode>()
                          where node.Value == fun.FUNCTION_ID
                          select node).FirstOrDefault();
        if (query != null) query.Checked = true;
      }
      //找尋功能
      foreach (var fun in auths)
      {
        TreeNode query = (from node in Treeview_Function.Nodes.OfType<TreeNode>()
                          from child in node.ChildNodes.OfType<TreeNode>()
                          where child.Value == fun.FUNCTION_ID
                          select child).FirstOrDefault();
        if (query != null)
        {
          query.Checked = true;
        }
      }

      gv_function_control.DataBind();

      CommonUtil.DisplayInfoMessage("已切換至使用者-" + username + "[" + userid + "]之選單權限!", lbl_message);
    }
    catch (Exception ex)
    {
      CommonUtil.DisplayErrorMessage(ex.Message, lbl_message);
    }

  }
  protected void gv_function_control_RowDataBound(object sender, GridViewRowEventArgs e)
  {
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
      e.Row.Visible = false;
      Label lbl_function_id = e.Row.FindControl("lbl_function_id") as Label;
      CheckBoxList cb_control = e.Row.FindControl("cb_control") as CheckBoxList;
      foreach (TreeNode node in Treeview_Function.CheckedNodes)
      {
        if (node.Value == lbl_function_id.Text)
        {
          e.Row.Visible = true;
          if (TreeView_Users.SelectedNode != null)
          {
            string userid = TreeView_Users.SelectedNode.Value;
            string functionID = lbl_function_id.Text;
            var auths = mis.MIS_AUTHORIZOR_AP
                .Where(a => a.USER_ID == userid && a.FUNCTION_ID == functionID)
                .Select(c => c.CONTROL_ID).Distinct().ToArray();
            cb_control.Items.Cast<ListItem>()
                .Where(c => auths.Contains(c.Value)).ToList()
                .ForEach(c => c.Selected = true);
          }
        }
      }
    }
  }

  protected void btn_search_Click(object sender, EventArgs e)
  {

    TreeView_Users.Nodes.Clear();

    try
    {
      if (string.IsNullOrEmpty(txt_dept.Text.Trim()) && string.IsNullOrEmpty(txt_userid.Text.Trim()))
      {
        //若是沒有輸入任何查詢條件，則USER全部列出
        LoadUserTreeView();
        TreeView_Users.ExpandDepth = -1;
      }
      else
      {

        OracleConnection cn =
          new OracleConnection(System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);

        if (!string.IsNullOrEmpty(txt_userid.Text.Trim()))
        {
          var user = mis.MIS_USER_AP.Where(u => u.USER_ID == txt_userid.Text.Trim()).SingleOrDefault();
          if (user != null)
          {
            string UserID = user.USER_ID;
            string DeptID = !string.IsNullOrEmpty(txt_dept.Text.Trim()) ? txt_dept.Text.Trim() : user.DEPT_ID;

            //DEPT + USER
            OracleDataAdapter da = new OracleDataAdapter(@"
SELECT A.USER_ID,A.USER_NAME, 
B.BI4101,SF_GET_DEPT_NAME(B.BI4101) AS DEPTNAME, 
B.BI4112,SF_GET_DEPT_NAME(B.BI4112) AS ENAME, 
B.BI4113,SF_GET_DEPT_NAME(B.BI4113) AS DNAME, 
B.BI4114,SF_GET_DEPT_NAME(B.BI4114) AS CNAME, 
B.BI4115,SF_GET_DEPT_NAME(B.BI4115) AS BNAME, 
B.BI4119,SF_GET_DEPT_NAME(B.BI4119) AS ANAME 
FROM MIS_USER_AP A 
INNER JOIN AS400_BIF41  B ON  B.BI4101 =A.DEPT_ID 
WHERE A.DEPT_ID = :DEPT_ID AND A.USER_ID  = :USER_ID
ORDER BY B.BI4119,B.BI4115,B.BI4114,B.BI4113, B.BI4112
", cn);
            da.SelectCommand.Parameters.AddWithValue(":DEPT_ID", DeptID);
            da.SelectCommand.Parameters.AddWithValue(":USER_ID", UserID);
            DataTable dt = new DataTable("UserTreeView");
            da.Fill(dt);
            if (dt.Rows.Count == 0)
            {
              CommonUtil.DisplayErrorMessage("查詢不到符合資料!", lbl_tvmsg);
            }
            else
            {
              //string UserID = dt.Rows[0][0].ToString();
              //string DeptID = dt.Rows[0][2].ToString();
              //先加入總公司(最上層)
              string AKey = dt.Rows[0][13].ToString();
              string AValue = dt.Rows[0][12].ToString();
              if (!string.IsNullOrEmpty(AValue))
              {
                TreeNode ANode = BuildDeptTreeView(TreeView_Users, AKey, AValue);
                if (DeptID == AValue)
                {
                  AddUserToTreeNodeByUserID(ANode, UserID);
                }
                //營總
                string BKey = dt.Rows[0][11].ToString();
                string BValue = dt.Rows[0][10].ToString();
                if (!string.IsNullOrEmpty(BValue))
                {
                  TreeNode BNode = BuildDeptTreeNode(ANode, BKey, BValue);
                  if (DeptID == BValue)
                  {
                    AddUserToTreeNodeByUserID(BNode, UserID);
                  }
                  //處級
                  string CKey = dt.Rows[0][9].ToString();
                  string CValue = dt.Rows[0][8].ToString();
                  if (!string.IsNullOrEmpty(CValue))
                  {
                    TreeNode CNode = BuildDeptTreeNode(BNode, CKey, CValue);
                    if (DeptID == CValue)
                    {
                      AddUserToTreeNodeByUserID(CNode, UserID);
                    }
                    //區級
                    string DKey = dt.Rows[0][7].ToString();
                    string DValue = dt.Rows[0][6].ToString();
                    if (!string.IsNullOrEmpty(DValue))
                    {
                      TreeNode DNode = BuildDeptTreeNode(CNode, DKey, DValue);
                      if (DeptID == DValue)
                      {
                        AddUserToTreeNodeByUserID(DNode, UserID);
                      }

                      //所級
                      string EKey = dt.Rows[0][5].ToString();
                      string EValue = dt.Rows[0][4].ToString();
                      if (!string.IsNullOrEmpty(EValue))
                      {
                        TreeNode ENode = BuildDeptTreeNode(DNode, EKey, EValue);
                        if (DeptID == EValue)
                        {
                          AddUserToTreeNodeByUserID(ENode, UserID);
                        }
                      }
                    }
                  }
                }
              }
            }
          }
          else
          {
            CommonUtil.DisplayErrorMessage("查詢不到符合資料!", lbl_tvmsg);
          }
        }
        else
        {
          //ONLY DEPT
          var dept = mis.MIS_DEPT_AP.Where(d => d.DEPT_ID == txt_dept.Text.Trim()).SingleOrDefault();
          if (dept != null)
          {
            string DeptID = txt_dept.Text.Trim();
            OracleDataAdapter da = new OracleDataAdapter(@"
SELECT A.USER_ID,A.USER_NAME, 
B.BI4101,SF_GET_DEPT_NAME(B.BI4101) AS DEPTNAME, 
B.BI4112,SF_GET_DEPT_NAME(B.BI4112) AS ENAME, 
B.BI4113,SF_GET_DEPT_NAME(B.BI4113) AS DNAME, 
B.BI4114,SF_GET_DEPT_NAME(B.BI4114) AS CNAME, 
B.BI4115,SF_GET_DEPT_NAME(B.BI4115) AS BNAME, 
B.BI4119,SF_GET_DEPT_NAME(B.BI4119) AS ANAME 
FROM MIS_USER_AP A 
INNER JOIN AS400_BIF41  B ON  B.BI4101 =A.DEPT_ID 
WHERE A.DEPT_ID = :DEPT_ID 
ORDER BY B.BI4119,B.BI4115,B.BI4114,B.BI4113, B.BI4112
", cn);
            da.SelectCommand.Parameters.AddWithValue(":DEPT_ID", DeptID);
            DataTable dt = new DataTable("UserTreeView");
            da.Fill(dt);
            if (dt.Rows.Count == 0)
            {
              CommonUtil.DisplayErrorMessage("查詢不到符合資料!", lbl_tvmsg);
            }
            else
            {
              //先加入總公司(最上層)
              string AKey = dt.Rows[0][13].ToString();
              string AValue = dt.Rows[0][12].ToString();
              if (!string.IsNullOrEmpty(AValue))
              {
                TreeNode ANode = BuildDeptTreeView(TreeView_Users, AKey, AValue);
                if (DeptID == AValue)
                {
                  AddUserToTreeNodeByDept(ANode, DeptID);
                }
                //營總
                string BKey = dt.Rows[0][11].ToString();
                string BValue = dt.Rows[0][10].ToString();
                if (!string.IsNullOrEmpty(BValue))
                {
                  TreeNode BNode = BuildDeptTreeNode(ANode, BKey, BValue);
                  if (DeptID == BValue)
                  {
                    AddUserToTreeNodeByDept(BNode, DeptID);
                  }
                  //處級
                  string CKey = dt.Rows[0][9].ToString();
                  string CValue = dt.Rows[0][8].ToString();
                  if (!string.IsNullOrEmpty(CValue))
                  {
                    TreeNode CNode = BuildDeptTreeNode(BNode, CKey, CValue);
                    if (DeptID == CValue)
                    {
                      AddUserToTreeNodeByDept(CNode, DeptID);
                    }
                    //區級
                    string DKey = dt.Rows[0][7].ToString();
                    string DValue = dt.Rows[0][6].ToString();
                    if (!string.IsNullOrEmpty(DValue))
                    {
                      TreeNode DNode = BuildDeptTreeNode(CNode, DKey, DValue);
                      if (DeptID == DValue)
                      {
                        AddUserToTreeNodeByDept(DNode, DeptID);
                      }

                      //所級
                      string EKey = dt.Rows[0][5].ToString();
                      string EValue = dt.Rows[0][4].ToString();
                      if (!string.IsNullOrEmpty(EValue))
                      {
                        TreeNode ENode = BuildDeptTreeNode(DNode, EKey, EValue);
                        if (DeptID == EValue)
                        {
                          AddUserToTreeNodeByDept(ENode, DeptID);
                        }
                      }
                    }
                  }
                }
              }
            }

          }
          else
          {
            CommonUtil.DisplayErrorMessage("查詢不到符合資料!", lbl_tvmsg);
          }
        }
        //TreeView_Users.ExpandDepth = 1;
        TreeView_Users.ExpandAll();
      }
    }
    catch (Exception ex)
    {
      CommonUtil.DisplayErrorMessage(ex.Message + " 請洽系統管理員!",lbl_tvmsg);      
    }
    TreeView_Users.PopulateNodesFromClient = true;
    TreeView_Users.Attributes.Add("onclick", "OnTreeClick(event)");

  }
  protected void cb_all_CheckedChanged(object sender, EventArgs e)
  {
    CheckBox cb = (CheckBox)sender;
    CheckBoxList cb_control =
       ((GridViewRow)cb.Parent.Parent).FindControl("cb_control") as CheckBoxList;
    if (cb.Checked)
    {     
      if (cb_control!=null)
      {
        cb_control.Items.Cast<ListItem>().ToList()
               .ForEach(c => c.Selected = true);       
      }      
    }
    else
    {
      if (cb_control != null)
      {
        cb_control.Items.Cast<ListItem>().ToList()
               .ForEach(c => c.Selected = false);        
      } 
    }
  }
}